getApp();

Page({
    data: {
        radioLoan: [ {
            name: "2018年6月1日利率上限（1.3倍）",
            value: "1.3"
        }, {
            name: "2018年6月1日利率上限（1.2倍）",
            value: "1.2"
        }, {
            name: "2018年6月1日利率上限（1.1倍）",
            value: "1.1"
        }, {
            name: "2018年6月1日利率下限（85折）",
            value: "0.85"
        }, {
            name: "2018年6月1日利率下限（7折）",
            value: "0.7"
        }, {
            name: "2018年6月1日基准利率",
            value: "1",
            checked: !0
        } ],
        radioItems: [ {
            name: "根据面积、单价计算",
            value: "1",
            checked: !0
        }, {
            name: "根据贷款总额计算",
            value: "2"
        } ],
        radioRepayment: [ {
            name: "等额本息",
            value: "1",
            checked: !0
        }, {
            name: "等额本金",
            value: "2"
        } ],
        array: [ "商业贷款", "公积金贷款", "组合型贷款" ],
        arrayNum: [ "2成", "3成", "4成", "5成", "6成", "7成", "8成", "9成" ],
        arrayYear: [ "1年（12期）", "2年（24期）", "3年（36期）", "4年（48期）", "5年（60期）", "6年（72期）", "7年（84期）", "8年（96期）", "9年（108期）", "10年（120期）", "11年（132期）", "12年（144期）", "13年（156期）", "14年（168期）", "15年（180期）", "16年（192期）", "17年（204期）", "18年（216期）", "19年（228期）", "20年（240期）", "25年（300期）", "30年（360期）" ],
        show: "block",
        radioLoanNum2: "3.25",
        showLoan1: "block",
        showLoan2: "none",
        loanTypeShow: "block",
        loanType3Show: "none",
        index: "",
        unitPrice: "",
        acreage: "",
        indexNum: "",
        countType: "1",
        loanCeiling: "",
        indexYear: "",
        repaymentType: "1",
        totalHousePayment: "",
        totalLoanCeiling: "",
        totalRepayment: "",
        interest: "",
        initialPayment: "",
        loanMonths: "",
        monthlyRepayment: "",
        firstRepayment: "",
        decrease: "",
        loanCount: "4.900",
        loanCountEnd: "4.900",
        loaNVal: "1",
        loanType: ""
    },
    onShareAppMessage: function(e) {
        return e.from, {
            title: "房贷计算器",
            path: "/pages/index/index"
        };
    },
    bindPickerYearChange: function(e) {
        0 == e.detail.value ? this.setData({
            radioLoanNum2: "2.75",
            loanCount: "4.35",
            loanCountEnd: Number(4.35 * Number(this.data.loaNVal)).toFixed(3)
        }) : 1 == e.detail.value || 2 == e.detail.value || 3 == e.detail.value || 4 == e.detail.value ? this.setData({
            radioLoanNum2: "2.75",
            loanCount: "4.75",
            loanCountEnd: Number(4.75 * Number(this.data.loaNVal)).toFixed(3)
        }) : this.setData({
            radioLoanNum2: "3.25",
            loanCount: "4.9",
            loanCountEnd: Number(4.9 * Number(this.data.loaNVal)).toFixed(3)
        }), this.setData({
            indexYear: e.detail.value
        });
    },
    bindPickerNumChange: function(e) {
        this.setData({
            indexNum: e.detail.value
        });
    },
    radioChange: function(e) {
        1 == e.detail.value ? this.setData({
            show: "block",
            show2: "none"
        }) : 2 == e.detail.value && this.setData({
            show2: "block",
            show: "none"
        });
        for (var a = this.data.radioItems, t = 0, n = a.length; t < n; ++t) a[t].checked = a[t].value == e.detail.value;
        this.setData({
            radioItems: a,
            countType: e.detail.value
        });
    },
    radioLoanChange: function(e) {
        for (var a = this.data.radioLoan, t = 0, n = a.length; t < n; ++t) a[t].checked = a[t].value == e.detail.value;
        var i = Number(this.data.loanCount), o = Number(e.detail.value);
        this.setData({
            loaNVal: o,
            radioLoan: a,
            loanCountEnd: Number(o * i).toFixed(3)
        });
    },
    radioRepaymentChange: function(e) {
        this.setData({
            repaymentType: e.detail.value
        });
        for (var a = this.data.radioRepayment, t = 0, n = a.length; t < n; ++t) a[t].checked = a[t].value == e.detail.value;
        this.setData({
            radioRepayment: a
        });
    },
    bindPickerChange: function(e) {
        this.setData({
            index: e.detail.value
        }), "0" === e.detail.value ? this.setData({
            showLoan1: "block",
            showLoan2: "none",
            loanTypeShow: "block",
            loanType3Show: "none"
        }) : "1" === e.detail.value ? this.setData({
            showLoan1: "none",
            showLoan2: "block",
            loanTypeShow: "block",
            loanType3Show: "none"
        }) : "2" === e.detail.value && this.setData({
            showLoan1: "block",
            showLoan2: "block",
            loanTypeShow: "none",
            loanType3Show: "block"
        });
    },
    bindUnitPrice: function(e) {
        this.setData({
            unitPrice: e.detail.value
        });
    },
    bindAcreage: function(e) {
        this.setData({
            acreage: e.detail.value
        });
    },
    bindLoanCeiling: function(e) {
        this.setData({
            loanCeiling: e.detail.value
        });
    },
    bindsyMoneyPrice: function(e) {
        this.setData({
            syMoney: e.detail.value
        });
    },
    bindgjjMoneyPrice: function(e) {
        this.setData({
            gjjMoney: e.detail.value
        });
    },
    bindCount: function(e) {
        var a = this.data.index, t = this.data.countType, n = this.data.unitPrice, i = this.data.acreage, o = this.data.indexNum ? Number(this.data.indexNum) + 2 : "", l = this.data.loanCeiling, s = "20" == this.data.indexYear ? 25 : "21" == this.data.indexYear ? 30 : this.data.indexYear ? Number(this.data.indexYear) + 1 : "", r = this.data.loanCountEnd, d = this.data.radioLoanNum2, u = this.data.repaymentType, m = this.data.syMoney, h = this.data.gjjMoney;
        if ("" != a) {
            if (0 == a || 1 == a && t) if (1 == t) {
                if ("" == n) return void wx.showToast({
                    title: "请输入单价",
                    icon: "none",
                    duration: 1e3
                });
                if ("" == i) return void wx.showToast({
                    title: "请输入面积",
                    icon: "none",
                    duration: 1e3
                });
                if ("" == o) return void wx.showToast({
                    title: "请输入按揭成数",
                    icon: "none",
                    duration: 1e3
                });
            } else if ("" == l) return void wx.showToast({
                title: "请输入贷款总额",
                icon: "none",
                duration: 1e3
            });
            if ("" != s) {
                if ("0" == a) if ("1" == t) {
                    var y = n * i, p = y * (o / 10), c = 12 * s, b = r / 100 / 12, v = y - p;
                    if ("1" == u) {
                        var f = p * Number(r) / 100 / 12 * Math.pow(1 + Number(r) / 100 / 12, c) / (Math.pow(1 + Number(r) / 100 / 12, c) - 1), N = f * c, w = N - p;
                        this.setData({
                            totalHousePayment: y,
                            totalLoanCeiling: p,
                            loanMonths: c,
                            monthlyRepayment: f,
                            initialPayment: v,
                            totalRepayment: N,
                            interest: w,
                            loanType: a
                        });
                    } else {
                        var g = p / c + p * b, R = p / c * b, C = (p / c + p * b + p / c * (1 + b)) / 2 * c - p, P = p + C;
                        this.setData({
                            totalHousePayment: y,
                            totalLoanCeiling: p,
                            loanMonths: c,
                            firstRepayment: g,
                            decrease: R,
                            initialPayment: v,
                            totalRepayment: P,
                            interest: C,
                            loanType: a
                        });
                    }
                } else if ("1" == u) {
                    var T = 1e4 * Number(l), A = 12 * s, L = T * Number(r) / 100 / 12 * Math.pow(1 + Number(r) / 100 / 12, A) / (Math.pow(1 + Number(r) / 100 / 12, A) - 1), D = L * A, M = D - T;
                    this.setData({
                        totalHousePayment: "",
                        totalLoanCeiling: T,
                        loanMonths: A,
                        monthlyRepayment: L,
                        initialPayment: "",
                        totalRepayment: D,
                        interest: M,
                        loanType: a
                    });
                } else {
                    var x = 1e4 * Number(l), k = 12 * s, j = r / 100 / 12, H = x / k + x * j, S = x / k * j, I = (x / k + x * j + x / k * (1 + j)) / 2 * k - x, E = x + I;
                    this.setData({
                        totalHousePayment: "",
                        totalLoanCeiling: x,
                        loanMonths: k,
                        firstRepayment: H,
                        decrease: S,
                        initialPayment: "",
                        totalRepayment: E,
                        interest: I,
                        loanType: a
                    });
                } else if ("1" == a) if ("1" == t) {
                    var Y = n * i, V = Y * (o / 10), B = 12 * s, F = d / 100 / 12, U = Y - V;
                    if ("1" == u) {
                        var J = V * Number(d) / 100 / 12 * Math.pow(1 + Number(d) / 100 / 12, B) / (Math.pow(1 + Number(d) / 100 / 12, B) - 1), q = J * B, O = q - V;
                        this.setData({
                            totalHousePayment: Y,
                            totalLoanCeiling: V,
                            loanMonths: B,
                            monthlyRepayment: J,
                            initialPayment: U,
                            totalRepayment: q,
                            interest: O,
                            loanType: a
                        });
                    } else {
                        var Q = V / B + V * F, z = V / B * F, G = (V / B + V * F + V / B * (1 + F)) / 2 * B - V, K = V + G;
                        this.setData({
                            totalHousePayment: Y,
                            totalLoanCeiling: V,
                            loanMonths: B,
                            firstRepayment: Q,
                            decrease: z,
                            initialPayment: U,
                            totalRepayment: K,
                            interest: G,
                            loanType: a
                        });
                    }
                } else if ("1" == u) {
                    var _ = 1e4 * Number(l), W = 12 * s, X = _ * Number(d) / 100 / 12 * Math.pow(1 + Number(d) / 100 / 12, W) / (Math.pow(1 + Number(d) / 100 / 12, W) - 1), Z = X * W, $ = Z - _;
                    this.setData({
                        totalHousePayment: "",
                        totalLoanCeiling: _,
                        loanMonths: W,
                        monthlyRepayment: X,
                        initialPayment: "",
                        totalRepayment: Z,
                        interest: $,
                        loanType: a
                    });
                } else {
                    var ee = 1e4 * Number(l), ae = 12 * s, te = d / 100 / 12, ne = ee / ae + ee * te, ie = ee / ae * te, oe = (ee / ae + ee * te + ee / ae * (1 + te)) / 2 * ae - ee, le = ee + oe;
                    this.setData({
                        totalHousePayment: "",
                        totalLoanCeiling: ee,
                        loanMonths: ae,
                        firstRepayment: ne,
                        decrease: ie,
                        initialPayment: "",
                        totalRepayment: le,
                        interest: oe,
                        loanType: a
                    });
                } else {
                    var se = 12 * s, re = r / 100 / 12, de = d / 100 / 12;
                    if ("1" == u) {
                        var ue = Number(m) + Number(h), me = m * Number(r) / 100 / 12 * Math.pow(1 + Number(r) / 100 / 12, se) / (Math.pow(1 + Number(r) / 100 / 12, se) - 1) + h * Number(d) / 100 / 12 * Math.pow(1 + Number(d) / 100 / 12, se) / (Math.pow(1 + Number(d) / 100 / 12, se) - 1), he = me * se, ye = he - ue;
                        this.setData({
                            totalHousePayment: "",
                            totalLoanCeiling: ue,
                            loanMonths: se,
                            monthlyRepayment: me,
                            initialPayment: "",
                            totalRepayment: he,
                            interest: ye,
                            loanType: a
                        });
                    } else {
                        var pe = Number(m) + Number(h), ce = m / se + m * re, be = h / se + h * de, ve = ce + be, fe = m / se * re, Ne = h / se * de, we = (m / se + m * re + m / se * (1 + re)) / 2 * se - m + ((h / se + h * de + h / se * (1 + de)) / 2 * se - h), ge = pe + we;
                        this.setData({
                            totalHousePayment: "",
                            totalLoanCeiling: pe,
                            loanMonths: se,
                            firstRepayment: ve,
                            firstRepaymentsy: ce,
                            firstRepaymentgjj: be,
                            decrease: "",
                            decreasesy: fe,
                            decreasegjj: Ne,
                            initialPayment: "",
                            totalRepayment: ge,
                            interest: we,
                            loanType: a
                        });
                    }
                }
                wx.navigateTo({
                    url: "/pages/result/result?totalHousePayment=" + this.data.totalHousePayment + "&totalLoanCeiling=" + this.data.totalLoanCeiling + "&loanMonths=" + this.data.loanMonths + "&monthlyRepayment=" + this.data.monthlyRepayment + "&initialPayment=" + this.data.initialPayment + "&totalRepayment=" + this.data.totalRepayment + "&interest=" + this.data.interest + "&repaymentType=" + u + "&firstRepayment=" + this.data.firstRepayment + "&firstRepaymentsy=" + this.data.firstRepaymentsy + "&firstRepaymentgjj=" + this.data.firstRepaymentgjj + "&decrease=" + this.data.decrease + "&decreasesy=" + this.data.decreasesy + "&decreasegjj=" + this.data.decreasegjj + "&loanType=" + this.data.loanType
                });
            } else wx.showToast({
                title: "请选择按揭年数",
                icon: "none",
                duration: 1e3
            });
        } else wx.showToast({
            title: "请选择贷款类别",
            icon: "none",
            duration: 1e3
        });
    },
    bindRefill: function(e) {
        this.setData({
            index: "",
            countType: "1",
            unitPrice: "",
            acreage: "",
            loanCeiling: "",
            indexYear: "",
            loanCountEnd: "4.900",
            radioLoanNum2: "3.25",
            radioLoan: [ {
                name: "2018年6月1日利率上限（1.3倍）",
                value: "1.3"
            }, {
                name: "2018年6月1日利率上限（1.2倍）",
                value: "1.2"
            }, {
                name: "2018年6月1日利率上限（1.1倍）",
                value: "1.1"
            }, {
                name: "2018年6月1日利率下限（85折）",
                value: "0.85"
            }, {
                name: "2018年6月1日利率下限（7折）",
                value: "0.7"
            }, {
                name: "2018年6月1日基准利率",
                value: "1",
                checked: !0
            } ],
            radioItems: [ {
                name: "根据面积、单价计算",
                value: "1",
                checked: !0
            }, {
                name: "根据贷款总额计算",
                value: "2"
            } ],
            radioRepayment: [ {
                name: "等额本息",
                value: "1",
                checked: !0
            }, {
                name: "等额本金",
                value: "2"
            } ]
        });
    },
    bindReward: function(e) {
        wx.previewImage({
            current: [ "http://www.liangmann.com/images/zs.png" ],
            urls: [ "http://www.liangmann.com/images/zs.png" ]
        });
    },
    bindJump: function(e) {},
    onLoad: function() {},
    getUserInfo: function(e) {}
});